clear all
set more off

cd            "/CGR_credit"
local out2 = "/CGR_credit/outregsQJE/"
use "step001_credit_QJE.dta"

tsset firm_id time

*** define treatment as given inflation TIMES treatment status
gen Treat=treatment_old*infl

* multiply by 100 mean utilization rate
gen    util_ratio_mean0 = util_ratio_mean * 100
drop   util_ratio_mean
rename util_ratio_mean0 util_ratio_mean

* rename variable and create first difference
rename util_ratio_mean ur_crl
gen dur_crl=d.ur_crl

*** winsorize utilizatior rate and delta(utilization rate)
winsor ur_crl, gen(ur_crl_w) p(0.02)
winsor dur_crl, gen(dur_crl_w) p(0.02)

*** winsorize firm expected price change
winsor dprez, gen(dprez_w) p(0.02)

*** transform variable sitimp5 into 3-category variable (not going to use it)
recode sitimp5 (1 2 = 1) (3 = 2) (4 5 = 3), gen(sitimp3)


local controls0=" l2.dprez_w  i.l2.sitimp5 i.l2.occtot i.l2.sitgen i.l2.promig i.l2.liquid  "	


 *** restrict the sample only to the period with treatment and control groups
keep if time>=208

***======================================================
*** 					IRF: cumulative [from levels]
***						No firm FE
***======================================================



***======================================================
*** 			responses by sector
***======================================================


*** one table by horizon
foreach sec in 1 2 3 {
	foreach hor in 1 {

		capture drop FC*	
		gen FC=f.primainfo + primainfo
	
		ivreghdfe FC `controls0' ///
			(l.it12_all = l.Treat) [aw=peso]  if setton3==`sec', ///
			ffirst dkraay(2) absorb(i.setton5#i.trimestre)
			
			
		if `hor'==1 & `sec'==1 {	
		outreg2 using "`out2'AppendixTable07_app_sector.dta", replace dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}
		else {
		outreg2 using "`out2'AppendixTable07_app_sector.dta", append dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}	

	}
}



***======================================================
*** 			responses by firm size
***======================================================

	
gen size=1 	if add_new<=99 
replace size=2 if add_new>99 & add_new<=299
replace size=3 if add_new>=300 & add_new~=.

	
*** one table by horizon
foreach sec in 1 2 3 {
	foreach hor in 1 {

		capture drop FC*	
		gen FC=f.primainfo + primainfo
		
		ivreghdfe FC `controls0' ///
			(l.it12_all = l.Treat) [aw=peso]  if size==`sec', ///
			ffirst dkraay(2) absorb(i.setton5#i.trimestre)
			
			
		if `hor'==1 & `sec'==1 {	
		outreg2 using "`out2'AppendixTable07_app_size.dta", replace dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}
		else {
		outreg2 using "`out2'AppendixTable07_app_size.dta", append dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}	

	}
}

	
***======================================================
*** 			responses by export share
***======================================================

gen exD=1 	if export==1
replace exD=2 if export==2
replace exD=3 if (export==3 | export==4) & export~=.


*** one table by horizon
foreach sec in 1 2 3 {
	foreach hor in 1 {

		capture drop FC*	
		gen FC=f.primainfo + primainfo
		
		ivreghdfe FC `controls0' ///
			(l.it12_all = l.Treat) [aw=peso]  if exD==`sec', ///
			ffirst dkraay(2) absorb(i.setton5#i.trimestre)
			
			
		if `hor'==1 & `sec'==1 {	
		outreg2 using "`out2'AppendixTable07_app_export.dta", replace dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}
		else {
		outreg2 using "`out2'AppendixTable07_app_export.dta", append dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}	

	}
}
*/
	
***======================================================
*** 			responses by geography
***======================================================

gen geo=1 	if (area==1 | area==2) & area~=.
replace geo=2 if area==3
replace geo=3 if  area==4


*** one table by horizon
foreach sec in 1 2 3 {
	foreach hor in 1 {

		capture drop FC*	
		gen FC=f.primainfo + primainfo
		
		ivreghdfe FC `controls0' ///
			(l.it12_all = l.Treat) [aw=peso]  if geo==`sec', ///
			ffirst dkraay(2) absorb(i.setton5#i.trimestre)
			
			
		if `hor'==1 & `sec'==1 {	
		outreg2 using "`out2'AppendixTable07_app_geo.dta", replace dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}
		else {
		outreg2 using "`out2'AppendixTable07_app_geo.dta", append dta dec(3) ctitle("`sec':`hor'") ///
			addstat("1st stage F stat", e(widstat))
		}	

	}
}				
		

